﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Unity.Errors.types;

namespace DbExecuter.DataLayer.commands
{
    public class TCommandBase
    {
        #region ctors
        public TCommandBase(SqlConnection connection)
        {
            _Connection = connection;
            _CommandParameters
                = new List<TCommandParameter>();
            lockObj = new object();
        }
        #endregion

        #region protected methods
        protected void ConstructCommand(string cmdText)
        {
            _Command = new SqlCommand(cmdText, _Connection);
            int parameterCount = _CommandParameters.Count;
            for (int i = 0; i < parameterCount; i++)
            {
                _Command.Parameters.Add
                    (_CommandParameters[i].SqlParameter);
            }
        }
        protected void OpenConnection()
        {
            lock (lockObj)
            {
                if (_Connection.State == ConnectionState.Closed)
                {
                    try
                    {
                        _Connection.Open();
                    }
                    catch (Exception ex)
                    {
                        SErrorManager.Exception()
                            .Throw(ex, new TErrorInfo(GetType(), "OpenConnection")
                            , "Opening connection failed.\nConnection String is {0}\nConnectionState is {1}"
                            , _Connection.ConnectionString
                            , _Connection.State.ToString());
                    }
                }
            }
        }
        protected void CloseConnection()
        {
            lock (lockObj)
            {
                if (_Connection.State == ConnectionState.Open)
                    try
                    {
                        _Connection.Close();
                    }
                    catch (Exception ex)
                    {
                        SErrorManager.Exception()
                            .Throw(ex, new TErrorInfo(GetType(), "CloseConnection")
                            , "Closing connection failed.\nConnection String is {0}\nConnectionState is {1}"
                            , _Connection.ConnectionString
                            , _Connection.State.ToString());
                    }
            }
        }
        #endregion

        #region private fields
        private Object lockObj;
        #endregion

        #region properties
        private SqlConnection _Connection;

        private SqlCommand _Command;
        protected SqlCommand Command
        {
            get { return _Command; }
        }

        private List<TCommandParameter> _CommandParameters;
        public List<TCommandParameter> CommandParameters
        {
            get { return _CommandParameters; }
            protected set { _CommandParameters = value; }
        }
        #endregion
    }
}
